import 'package:flutter/material.dart';
import 'package:pp_uikit/theme/pp_default_theme.dart';

abstract class BannerIndicator {
  Widget build(BuildContext context, int index, int itemCount);
}

/// Circular style indicator
class CircleBannerIndicator extends _BannerIndicator {
  CircleBannerIndicator({
    EdgeInsetsGeometry? padding,
    double spacing = 6.0,
    double radius = 3,
    Color itemColor = const Color(0xFFF1F1F1),
    Color? itemActiveColor,
    BoxShadow? shadow,
  }) : super(
          padding: padding,
          spacing: spacing,
          radius: radius,
          itemColor: itemColor,
          itemWidth: radius * 2,
          itemHeight: radius * 2,
          itemActiveColor: itemActiveColor,
          itemShape: BoxShape.circle,
          shadow: shadow,
        );
}

class _BannerIndicator implements BannerIndicator {
  _BannerIndicator({
    this.spacing = 0.0,
    this.itemColor = const Color(0xFFF1F1F1),
    this.itemActiveColor,
    required this.radius,
    required this.itemWidth,
    required this.itemHeight,
    required this.itemShape,
    this.padding,
    this.shadow,
  });
  final double radius;

  /// How much space to place between children in a run in  horizontal direction.
  ///
  /// For example, if [spacing] is 10.0, the children will be spaced at least
  /// 10.0 logical pixels apart in horizontal direction.
  final double spacing;

  /// The indicator color of inactive state
  final Color itemColor;

  /// The indicator color of active state
  final Color? itemActiveColor;

  final double itemWidth;
  final double itemHeight;
  final BoxShape itemShape;
  final EdgeInsetsGeometry? padding;

  /// 阴影
  /// BoxShadow(
  ///   color: Colors.black12,   // 阴影颜色
  ///   offset: Offset(0, 2), // 阴影xy轴偏移量
  ///   blurRadius: 4, // 阴影模糊程度
  ///   spreadRadius: 0, // 阴影扩散程度
  /// ),
  final BoxShadow? shadow;

  @override
  Widget build(BuildContext context, int index, int itemCount) {
    if (itemCount == 1) return const SizedBox(width: .0, height: .0);

    var children = List.generate(itemCount, (childIndex) {
      Color color = itemColor;
      var width = itemWidth;
      if (childIndex == index) {
        width = itemWidth * 2;
        color = itemActiveColor ?? PPDefaultTheme.instance.brandPrimary;
      }
      return Container(
        width: width,
        height: itemHeight,
        decoration: BoxDecoration(
          color: color,
          // shape: shape,
          borderRadius: BorderRadius.all(Radius.circular(radius)),
          boxShadow: shadow != null ? [shadow!] : null,
        ),
      );
    });
    return Padding(
      padding: padding ?? const EdgeInsets.all(10.0),
      child: Wrap(
        alignment: WrapAlignment.center,
        runSpacing: spacing,
        spacing: spacing,
        children: children,
      ),
    );
  }
}

class PPIndicator extends StatefulWidget {
  const PPIndicator({
    Key? key,
    required this.initPage,
    required this.itemCount,
    required this.indicator,
  }) : super(key: key);

  @override
  PPIndicatorState createState() => PPIndicatorState();

  final BannerIndicator indicator;
  final int itemCount;
  final int initPage;
}

class PPIndicatorState extends State<PPIndicator> {
  int _index = 0;

  @override
  void initState() {
    super.initState();
    _index = widget.initPage;
  }

  update(int index) {
    setState(() {
      _index = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return widget.indicator.build(context, _index, widget.itemCount);
  }
}
